package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.module.pojo.UserBodyData;
import com.example.fitnessservice.service.BodyDataService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class BodyDataController {
    @Autowired
    private BodyDataService bodyDataService;

    //查询身体数据
    @GetMapping("/bodyData/{userId}")
    public Result findBodyDataById(@PathVariable Integer userId){
        List<UserBodyData> userBodyDatas = bodyDataService.findBodyDataById(userId);

        Result result = new Result();
        if(userBodyDatas != null){
            result.setCode(200);
            result.setMsg("身体数据查询成功");
            result.setData(userBodyDatas);
        }else {
            result.setCode(500);
            result.setMsg("身体数据查询失败");
        }

        return result;
    }

    @PostMapping("/bodyData")
    public Result addBodyData(@RequestBody UserBodyData userBodyData) throws ParseException {
        System.out.println(userBodyData);
        boolean flag = bodyDataService.addBodyDataById(userBodyData);

        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("添加身体数据成功");
        }else {
            result.setCode(500);
            result.setMsg("添加身体数据失败");
        }
        return result;
    }

    @GetMapping("/bodyData/{userId}/{currentPage}/{pageSize}")
    public Result findBodyDataByPage(@PathVariable Integer userId, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<UserBodyData> bodyDataList = bodyDataService.findBodyDataByPage(userId, currentPage, pageSize);

        Result result = new Result();
        if (bodyDataList != null) {
            PageInfo<UserBodyData> pageInfo = new PageInfo<>(bodyDataList);
            result.setCode(200);
            result.setMsg("身体数据查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("身体数据查询失败");
        }
        return result;
    }
}
